/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bl;

import Config.Localization;
import da.DALoanContact;
import entities.LoanContact;
import entities.User;
import java.sql.Date;
import java.util.ArrayList;
import javax.swing.JOptionPane;

/**
 *
 * @author Bich Tran Van
 */
public class BLLoanContact {
    DALoanContact daLoanContact;

    public BLLoanContact() {
        daLoanContact = new DALoanContact();
    }

    //get all loan type
    public ArrayList<LoanContact> getAll(User user) throws Exception {
        return daLoanContact.getAll(user);
    }
    
    public ArrayList<LoanContact> getLoanContactSelect(int select , User user) throws Exception {
        return daLoanContact.getLoanContactSelect(select,user);
    }
    
    public ArrayList<LoanContact> getByID(User user,String loanid) throws Exception {
        int id = 0;
        boolean check = false;
        try {
            id = Integer.parseInt(loanid);
            check = true;
        } catch (Exception ex) {
            check = false;
            //ex.printStackTrace();
        }
        if (check) {
            return daLoanContact.getByLoanID(id, user);
        } else {
            JOptionPane.showMessageDialog(null, Localization.getMessage("SearchID"), "Error", JOptionPane.ERROR_MESSAGE);
            return daLoanContact.getAll(user);
        }
    }
    
    public ArrayList<LoanContact> getByCustomerID(User user,String customerID) throws Exception {
        int id = 0;
        boolean check = false;
        try {
            id = Integer.parseInt(customerID);
            check = true;
        } catch (Exception ex) {
            check = false;
            //ex.printStackTrace();
        }
        if (check) {
            return daLoanContact.getByCustomerID(id, user);
        } else {
            JOptionPane.showMessageDialog(null, Localization.getMessage("SearchID"), "Error", JOptionPane.ERROR_MESSAGE);
            return daLoanContact.getAll(user);
        }
    }
    
    public ArrayList<LoanContact> getByAmount(User user,String fAmount, String tAmount) throws Exception {
        double fromAmount = 0;
        double toAmount = 0;
        boolean check = true;
        
        String error = "Error : \n"
                + "----------------------------------";

        //check information
        if (fAmount.equals("")) {
            check = false;
            error = error + "\nFrom Amount " + Localization.getMessage("Null");
        } else {
            try {
                fromAmount = Double.parseDouble(fAmount);
            } catch (Exception ex) {
                check = false;
                error = error + "\nFrom Amount invalid." ;
                //ex.printStackTrace();
            }
        }
        
        if (tAmount.equals("")) {
            check = false;
            error = error + "\nTo Amount " + Localization.getMessage("Null");
        } else {
            try {
                toAmount = Double.parseDouble(tAmount);
            } catch (Exception ex) {
                check = false;
                error = error + "\nTo Amount invalid." ;
                //ex.printStackTrace();
            }
        }
        
        if (check) {
            return daLoanContact.getByAmount(fromAmount, toAmount, user);
        } else {
            JOptionPane.showMessageDialog(null, error, "Error", JOptionPane.ERROR_MESSAGE);
            return daLoanContact.getAll(user);
        }
    }
    
    public ArrayList<LoanContact> getByDate(User user,Date fromDate, Date toDate) throws Exception {
        
        boolean check = true;
        String error = "Error : \n"
                + "----------------------------------";

        //check information
        if (fromDate == null) {
            check = false;
            error = error + "\nFrom Date " + Localization.getMessage("Null");
        } 
        
        
        
        if (toDate == null) {
            check = false;
            error = error + "\nTo Date " + Localization.getMessage("Null");
        }
        
        if (check) {
            return daLoanContact.getByDate(fromDate, toDate, user);
        } else {
            JOptionPane.showMessageDialog(null, error, "Error", JOptionPane.ERROR_MESSAGE);
            return null;
        }
    }
    
    public boolean insert(int customerID, int userID, int loantypeID, String amount, String period, Date dateStart) throws Exception {
        int result = 0;
        boolean check = true;
        double amo = 0 ;
        int peri= 0;
        String error = "Error : \n"
                + "----------------------------------";

        //check information
        if (amount.equals("")) {
            check = false;
            error = error + "\nAmount " + Localization.getMessage("Null");
        } else {
            try {
                amo = Double.parseDouble(amount);
                if(amo <=0){
                    check = false;
                    error = error + "\nAmount can not less than 0.";
                }
            } catch (Exception ex) {
                check = false;
                error = error + "\nAmount invalid";
                //ex.printStackTrace();
            }
        }

        if (period.equals("")) {
            check = false;
            error = error + "\nPeriod " + Localization.getMessage("Null");
        } else {
            try {
                peri = Integer.parseInt(period);
                if(peri <=0){
                    check = false;
                    error = error + "\nPeriod can not less than 0.";
                }
            } catch (Exception ex) {
                check = false;
                error = error + "\nPeriod invalid.";
                //ex.printStackTrace();
            }
        }

        if(dateStart == null){
            check = false;
            error = error + "\nDate Start " + Localization.getMessage("Null");
        }else{
                java.util.Date a = new java.util.Date();
             java.sql.Date da ;
                 da = new java.sql.Date(a.getTime());
            if(dateStart.before(da)){
            check = false;
            error = error + "\nDate Start can not less than the day";
            }
        }
        if (check) {
            result = daLoanContact.insert(customerID, loantypeID, userID, amo, peri,dateStart);
            if (result == 0) {
                JOptionPane.showMessageDialog(null, Localization.getInstance().getMessage("InsertError"), "Error", JOptionPane.ERROR_MESSAGE);
                return false;
            }else {
                return true;
            }
        } else {
            JOptionPane.showMessageDialog(null, error, "Error", JOptionPane.ERROR_MESSAGE);
            return false;
        }
       
    }
    
    public int accecpted(int id , int loantypeid,User user){
        int result = 0;
        try{
            result =  daLoanContact.accecpted(id,user.getUserId());
            daLoanContact.calculatePayment(id, loantypeid);
        }catch(Exception ex){
            
        }
        return result;
    }
    
    public int notAccecpted(int id , User user){
        try{
            return  daLoanContact.notAccecpted(id,user.getUserId());
        }catch(Exception ex){
            return 0;
        }
    }
    
    
}
